////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008 Duykham.
// All Rights Reserved.
// Author: Duykham (duykham@gmail.com)
// Modified: 20081030
// TODO: load sensors information from XML file
//
////////////////////////////////////////////////////////////////////////////////
package dk.agrasys
{
	import flash.events.*;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IViewCursor;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.utils.ArrayUtil;

	/**
	 * Load sensors information from an XML file 
	 * @author Duykham
	 * 
	 */	
	public class LoadSensor
	{
		private var service:HTTPService;

		/**
		 * Array contains information of sensors 
		 */        
        [Bindable]
		public var sensors:ArrayCollection;

		/**
		 * Constructor 
		 * @param url
		 * 
		 */		
		public function LoadSensor(url:String)
		{
			service = new HTTPService();
			service.url = url;
			service.addEventListener(ResultEvent.RESULT, resultHandler);
			service.send();
		}

		/**
		 * Handle result when data is loaded  
		 * @param event
		 * 
		 */		
		private function resultHandler(event:ResultEvent):void
		{
		    var result:ArrayCollection = event.result.ss.s is ArrayCollection
		        ? event.result.ss.s as ArrayCollection
		        : new ArrayCollection(ArrayUtil.toArray(event.result.ss.s));    
		    var temp:ArrayCollection = new ArrayCollection();
		    var cursor:IViewCursor = result.createCursor();
		    while (!cursor.afterLast)
		    {
		    	var s:Object = new Sensor(cursor.current);
		        temp.addItem({id:s.id, name:s.name, xPos:s.xPos, yPos:s.yPos});
		        cursor.moveNext();
		    }
		    sensors = temp;
		}
	}
	
}